import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { loadEnv } from 'vite'; // Vite 内置的 loadEnv 方法
import path from 'path';


// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  // Load environment variables based on the current mode
  const env = loadEnv(mode, process.cwd(), 'VITE_');
  // const env = loadEnv(mode, process.cwd(), ''); 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀
  console.log("mode:", mode); // 打印当前模式
  // console.log("process.cwd():", process.cwd()); // 打印当前工作目录
  console.log("env:", env); // 打印环境变量
  // console.log("env.VITE_HOST", env.VITE_HOST); // 打印环境变量


  return {
    base: env.VITE_BASE_URL || '/', // Set the base URL based on the environment variable
    plugins: [react()],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      }
    },
    server: {
      port: parseInt(env.VITE_PORT) || 5173, // Default to port 5173 if not specified
      host: env.VITE_HOST || 'localhost', // Optional: Set a specific host if needed
      open: true, // Automatically open the app in the browser on startup
      proxy: {
        '/douyin/api/dev': {
          target: env.VITE_API_URL || 'http://localhost:3000', // Set the API URL based on the environment variable
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },

        '/douyin/api/prod': {
          target: env.VITE_API_URL || 'http://localhost:3000', // Set the API URL based on the environment variable
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },

  }

})
